

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class game
{
    public static Scanner cin = new Scanner(System.in);
    int i = 1;
    void operate()
    {
        System.out.println("欢迎来到猜数字游戏"+
                "\n"+"输入'enter'进入游戏;"+
                "\n"+"输入'exit'退出游戏;"+
                "\n"+"输入'record'最新的游戏记录;"+
                "\n"+"输入'answer'显示游戏的正确答案;"+
                "\n"+"输入'clear'清除记录;"+"\n");
        System.out.println("请选择上面选项");//游戏菜单
    }
    public void record(int i) throws IOException
    {
        File file = new File("D://test.txt");
        if(!file.exists()) file.createNewFile();
        FileOutputStream out  = new FileOutputStream(file);

        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        out.write (("最新记录"+sd.format(new Date())+" "+ + i+"次").getBytes());
        out.close();
    }
    public void playGame( int number)
    {



        System.out.println("游戏开始,请输入猜想的数字范围在0~99");

        try {

            while(true)
            {
                System.out.println("如果想知道本次游戏的答案，请输入answer");
                String n  = cin.next();
                if(n.equals("answer")){System.out.println("正确数字为："+number);record(i);break;}
                else if(Integer.parseInt(n)<0 ||Integer.parseInt(n)>99)System.out.println("输入有误，请输入0-99范围内");
                else if(Integer.parseInt(n)< number){System.out.println("你猜的数字偏小");i++;}
                else if(Integer.parseInt(n)> number){System.out.println("你猜的数字偏大");i++;}
                else if(Integer.parseInt(n) == number)
                {
                    System.out.println("你猜对了,猜了"+i+"次\n");
                    record(i);
                    i = 0;
                    break;
                }
                else System.out.println("先把数字猜完！");

            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }



    }
    static void showRecord()
    {
        try {
            File file = new File("D://test.txt");
            if(!file.exists())
            {
                file.createNewFile();
                System.out.println("没有记录!");
                return;
            }
            BufferedReader br = new BufferedReader(new FileReader(file));
            String str = br.readLine();
            if(str == null)System.out.println("没有记录!");
            else System.out.println(str);


            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    void clearRecord()  {
        try {
            File file = new File("D://test.txt");
            if(!file.exists()) file.createNewFile();
            FileOutputStream out  = new FileOutputStream(file);

            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            out.write ("".getBytes());
            out.close();
            System.out.println("清除成功！");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void main(String[] args)
    {


        game r = new game();

        while(true)
        {
            r.operate();
            String op = cin.next();
            int num = (int)(Math.random()*100);

            if(op.equals("enter")) { r.playGame(num);}
            else if(op.equals("exit")){break;}
            else if(op.equals("record")){r.showRecord();}
            else if(op.equals("clear")){r.clearRecord();}
            else if(op.equals("answer")){System.out.println("正确答案是："+num);}

            else {System.out.println("输入有误，请重新输入");}

        }


    }
}
